package homework.yuandan.exception;

import homework.yuandan.common.ResponseEnum;
import homework.yuandan.common.vo.ResultVO;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.MalformedJwtException;
import io.jsonwebtoken.SignatureException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author polly
 * @date 2020-12-14 15:29
 * <p>
 * 1：处理业务逻辑异常（例如登录时用户名密码错误等）
 * 2：处理非业务逻辑异常例如算数异常，数据库异常等
 */
@RestControllerAdvice
public class ExceptionAdvice {

    @ExceptionHandler(AppException.class)
    public ResultVO<Void> doAppException(AppException ex) {
        return new ResultVO<>(ex.getCode(), ex.getMessage());
    }


    @ExceptionHandler({MalformedJwtException.class, SignatureException.class})
    public ResultVO<Void> doJwtException(Exception ex) {
        ex.printStackTrace();
        return new ResultVO<Void>(ResponseEnum.TOKEN_ERROR);
    }

    @ExceptionHandler(ExpiredJwtException.class)
    @ResponseBody
    public ResultVO<Void> doException(ExpiredJwtException ex) {
        ex.printStackTrace();
        return new ResultVO<Void>(ResponseEnum.TOKEN_TIMEOUT);

    }
    /*@ExceptionHandler(Exception.class)
    public ResultVO<Void> doException(Exception ex) {
        return new ResultVO<>(500, "服务器错误");
    }*/
}